<html>
<head><meta charset="utf-8"><title>llvm abs intrinsic · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html">llvm abs intrinsic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227862813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227862813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227862813">(Feb 26 2021 at 00:25)</a>:</h4>
<p>Could someone with LLVM knowledge point me to where the <code>abs</code> intrinsic is defined in the LLVM source? Context: I'm trying to learn more about LLVM, and I noticed how <code>@llvm.abs</code> is optimized in different ways depending on the target, so I thought it would be a good start to understanding LLVM better :)</p>



<a name="227862983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227862983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227862983">(Feb 26 2021 at 00:26)</a>:</h4>
<p>I found <code>llvm/lib/CodeGen/IntrinsicLowering.cpp</code>, but that doesn't seem to mention the <code>abs</code> intrinsic.</p>



<a name="227863117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863117">(Feb 26 2021 at 00:28)</a>:</h4>
<p>I also found <a href="https://llvm.org/docs/ExtendingLLVM.html#adding-a-new-intrinsic-function">https://llvm.org/docs/ExtendingLLVM.html#adding-a-new-intrinsic-function</a>, but still couldn't find the intrinsic in the places it mentioned.</p>



<a name="227863229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863229">(Feb 26 2021 at 00:29)</a>:</h4>
<p>I'm especially curious to see the codegen for <code>@llvm.abs</code> on RISC-V, since it seems very clever.</p>



<a name="227863361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863361">(Feb 26 2021 at 00:31)</a>:</h4>
<p>Clang seems to be really great at recognizing that my function computes an absolute value (and I don't think it's looking at the name <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span>) and is able to convert it into <code>@llvm.abs</code> which then has target-specific implementations. <a href="https://godbolt.org/z/qE8xPW">https://godbolt.org/z/qE8xPW</a></p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="kt">int</span> <span class="nf">abs</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="o">-</span><span class="n">x</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">return</span> <span class="n">x</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div>



<a name="227863583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863583">(Feb 26 2021 at 00:33)</a>:</h4>
<p>Looks like rustc is able to do the same recognition, though maybe it's actually LLVM in both cases. <a href="https://rust.godbolt.org/z/zG4sb6">https://rust.godbolt.org/z/zG4sb6</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![no_std]</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">abs</span><span class="p">(</span><span class="n">x</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">-</span><span class="n">x</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u32</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="227863600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alex Gaynor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863600">(Feb 26 2021 at 00:34)</a>:</h4>
<p>Yeah, that'll be LLVM's optimizers doing that work.</p>



<a name="227863693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227863693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227863693">(Feb 26 2021 at 00:34)</a>:</h4>
<p>LLVM's intelligence really amazes and inspires me <span aria-label="grinning face with smiling eyes" class="emoji emoji-1f601" role="img" title="grinning face with smiling eyes">:grinning_face_with_smiling_eyes:</span></p>



<a name="227864309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227864309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227864309">(Feb 26 2021 at 00:42)</a>:</h4>
<p>You want to grep for <code>ISD::ABS</code> I think.</p>



<a name="227864568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227864568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227864568">(Feb 26 2021 at 00:45)</a>:</h4>
<p>Grep for <code>Intrinsic::abs</code> in <code>CodeGen/SelectionDAG/SelectionDAGBuilder.cpp</code> for where it becomes a DAG node.</p>



<a name="227864722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227864722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227864722">(Feb 26 2021 at 00:47)</a>:</h4>
<p>RISCV does not appear to have target-specific lowering code, so it goes through generic paths.</p>



<a name="227865302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865302">(Feb 26 2021 at 00:52)</a>:</h4>
<p>The RISCV impl you're seeing appears to be the most generic lowering LLVM has: <a href="https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp#L7047">https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp#L7047</a></p>



<a name="227865309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865309">(Feb 26 2021 at 00:52)</a>:</h4>
<p>Thanks!</p>



<a name="227865326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865326">(Feb 26 2021 at 00:53)</a>:</h4>
<p>Hmm, I did <code>rg -i Intrinsic::abs llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp</code> but got nothing.</p>



<a name="227865353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865353">(Feb 26 2021 at 00:53)</a>:</h4>
<p>Just <code>::abs</code> returns:</p>
<div class="codehilite" data-code-language="Bash Session"><pre><span></span><code><span class="gp">$</span> rg -i ::abs llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
<span class="go">3238:      Opc = ISD::ABS;</span>
</code></pre></div>



<a name="227865451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865451">(Feb 26 2021 at 00:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227865302">said</a>:</p>
<blockquote>
<p>The RISCV impl you're seeing appears to be the most generic lowering LLVM has: <a href="https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp#L7047">https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp#L7047</a></p>
</blockquote>
<p>Ah, so that's how it figures out to <code>&gt;&gt; 31</code>:</p>
<div class="codehilite" data-code-language="C++"><pre><span></span><code>  <span class="n">SDValue</span> <span class="n">Shift</span> <span class="o">=</span>
      <span class="n">DAG</span><span class="p">.</span><span class="n">getNode</span><span class="p">(</span><span class="n">ISD</span><span class="o">::</span><span class="n">SRA</span><span class="p">,</span> <span class="n">dl</span><span class="p">,</span> <span class="n">VT</span><span class="p">,</span> <span class="n">Op</span><span class="p">,</span>
                  <span class="n">DAG</span><span class="p">.</span><span class="n">getConstant</span><span class="p">(</span><span class="n">VT</span><span class="p">.</span><span class="n">getScalarSizeInBits</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dl</span><span class="p">,</span> <span class="n">ShVT</span><span class="p">));</span>
</code></pre></div>



<a name="227865460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865460">(Feb 26 2021 at 00:54)</a>:</h4>
<p>I meant <a href="https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L6399">https://github.com/llvm/llvm-project/blob/d7fca3f0bf176536fc8769ad02a18884d993d7ce/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L6399</a></p>



<a name="227865510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865510">(Feb 26 2021 at 00:55)</a>:</h4>
<p>Weird, it's not in my local <code>rust/llvm-project</code>.</p>



<a name="227865533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865533">(Feb 26 2021 at 00:55)</a>:</h4>
<p>make sure your checkout is not on master, that's probably ancient.</p>



<a name="227865546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865546">(Feb 26 2021 at 00:55)</a>:</h4>
<p>You want one of the branches for LLVM11 or LLVM12</p>



<a name="227865655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865655">(Feb 26 2021 at 00:56)</a>:</h4>
<p>It looks like it's a month old (Jan 20)</p>



<a name="227865710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865710">(Feb 26 2021 at 00:57)</a>:</h4>
<p>Btw, how do people come up with these optimization tricks? I wouldn't even know where to start <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">abs:</span>                                    <span class="c1"># @abs</span>
        <span class="nf">srai</span>    <span class="no">a1</span><span class="p">,</span> <span class="no">a0</span><span class="p">,</span> <span class="mi">31</span>
        <span class="nf">add</span>     <span class="no">a0</span><span class="p">,</span> <span class="no">a0</span><span class="p">,</span> <span class="no">a1</span>
        <span class="nf">xor</span>     <span class="no">a0</span><span class="p">,</span> <span class="no">a0</span><span class="p">,</span> <span class="no">a1</span>
        <span class="nf">ret</span>
</code></pre></div>



<a name="227865821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227865821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227865821">(Feb 26 2021 at 00:58)</a>:</h4>
<p>As somebody who has implemented generic lowering for overflowing unsigned multiplication for arbitrary width integers in LLVM, its probably involves expressing the problem as a math equation first.</p>



<a name="227866068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866068">(Feb 26 2021 at 01:01)</a>:</h4>
<p>Which typically will start involving operations modulo bitwidth and those get confusing fast ^^</p>



<a name="227866181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866181">(Feb 26 2021 at 01:03)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> there are a large number of well explored algorithms like these written down in various books and guides. PPC compiler writer's guide is one of such collections, knuth's book is another.</p>



<a name="227866236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866236">(Feb 26 2021 at 01:03)</a>:</h4>
<p><a href="https://graphics.stanford.edu/~seander/bithacks.html">https://graphics.stanford.edu/~seander/bithacks.html</a> is another classic</p>



<a name="227866609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866609">(Feb 26 2021 at 01:07)</a>:</h4>
<p>Also <a href="https://www.amazon.com/Hackers-Delight-2nd-Henry-Warren/dp/0321842685">Hacker's delight</a></p>



<a name="227866615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866615">(Feb 26 2021 at 01:07)</a>:</h4>
<p>This makes me wonder. To me personally the strength reduction of division/remainder by constant (to shifts and multiplies) always seemed like the most non-obvious but clever transformations out there.</p>



<a name="227866720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866720">(Feb 26 2021 at 01:08)</a>:</h4>
<p>So I wonder if there are more widely applied and equally convoluted equivalences out there.</p>



<a name="227866743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866743">(Feb 26 2021 at 01:08)</a>:</h4>
<p>I tried looking at ASM for that for a bunch of constants to figure out the pattern, without much success. Is there a good reference for it?</p>



<a name="227866795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866795">(Feb 26 2021 at 01:09)</a>:</h4>
<p>ppc compiler writer's guide has a good explanation on how to implement the transformation.</p>



<a name="227866804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866804">(Feb 26 2021 at 01:09)</a>:</h4>
<p>that's how I "got" it.</p>



<a name="227866820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866820">(Feb 26 2021 at 01:09)</a>:</h4>
<p>/me looks up the page.</p>



<a name="227866998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227866998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227866998">(Feb 26 2021 at 01:11)</a>:</h4>
<p>section 3.2.3.5, explanation starts on page 53 of <a href="https://cr.yp.to/2005-590/powerpc-cwg.pdf">https://cr.yp.to/2005-590/powerpc-cwg.pdf</a></p>



<a name="227867164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227867164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227867164">(Feb 26 2021 at 01:13)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227865710">said</a>:</p>
<blockquote>
<p>Btw, how do people come up with these optimization tricks? I wouldn't even know where to start <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
</blockquote>
<p>For that one in particular, it's using the fairly well known trick that arithmetic-shift-right by one less than the bit width gives you a mask of all zeros for positive numbers or all ones for negative numbers, because it replaces every bit with the sign bit.  One can use that to make a "poor man's" conditional move, and then the rest is probably just simplifications, remembering that <code>-x == ~x + 1</code> in 2s-complement.</p>
<p>(<code>x ^ (x ASHR 31)</code> is abs in 1s-complement, so then it's just a matter of doing a fixup for the off-by-one.  You'll see a similar idea in &lt;<a href="https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.total_cmp">https://doc.rust-lang.org/nightly/std/primitive.f32.html#method.total_cmp</a>&gt;)</p>



<a name="227867556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227867556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227867556">(Feb 26 2021 at 01:18)</a>:</h4>
<p>Oh, I was actually thinking of a slightly different optimization <span class="user-mention" data-user-id="123586">@nagisa</span> , although that one is interesting too. Have you seen multiplication by constants reduced to shift and add/sub? I think it's not as relevant on modern PCs since the relative cost of multiply over add is not as much as it used to be, but it's interesting to see</p>



<a name="227867727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227867727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jessa0 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227867727">(Feb 26 2021 at 01:20)</a>:</h4>
<p>sorry, lurker here, but is this optimization just using the multiplicative inverse modulo 2^n? which would be calculated using the extended euclidean algorithm.</p>



<a name="227867731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227867731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227867731">(Feb 26 2021 at 01:20)</a>:</h4>
<p>I'm sure the trick for turning division by constants into multiplication by constants can be interpreted as finding approximate modular inverses in the ring Z/2^64 (you don't always get exact inverses because 2^64 isn't prime)</p>



<a name="227867954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227867954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227867954">(Feb 26 2021 at 01:23)</a>:</h4>
<p>Personally, what I would really like to read a book on is not a laundry list of optimizations but a collection of heuristics for when to apply them. This latter information is much harder to obtain and is the most valuable thing to learn from production compilers IMO</p>



<a name="227868194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868194">(Feb 26 2021 at 01:25)</a>:</h4>
<p>especially inlining and register allocation, which are straightforward in principle but leave a lot of choice to the implementer</p>



<a name="227868236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868236">(Feb 26 2021 at 01:26)</a>:</h4>
<p>I don't recall seeing multiplications by constants turning into shifts except for powers of two, but only because I haven't ever looked at architectures where this would be beneficial. But from your description I intuit you'd do is something like, say <code>7x = 4x + 2x + x</code>?</p>



<a name="227868266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868266">(Feb 26 2021 at 01:26)</a>:</h4>
<p>yes, but you can do better than that with subtraction</p>



<a name="227868281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868281">(Feb 26 2021 at 01:26)</a>:</h4>
<p>I will see if I can show an example</p>



<a name="227868284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868284">(Feb 26 2021 at 01:26)</a>:</h4>
<p>Right.</p>



<a name="227868311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868311">(Feb 26 2021 at 01:26)</a>:</h4>
<p>yeah like <code>7x = 8x - x</code> reduces number of terms/operations.</p>



<a name="227868463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868463">(Feb 26 2021 at 01:28)</a>:</h4>
<p>Sounds fairly reasonable that you'd be able to find pretty optimal solution, I would guess using at most <code>log(n)</code> terms where <code>n</code> is number of bits of operation.</p>



<a name="227868553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868553">(Feb 26 2021 at 01:29)</a>:</h4>
<p><a href="https://www.godbolt.org/z/ovrY5d">https://www.godbolt.org/z/ovrY5d</a></p>



<a name="227868632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868632">(Feb 26 2021 at 01:30)</a>:</h4>
<p>Most constants don't optimize, I think on x86 gcc only does it for numbers of the form 2^n+-2^m, and clang even less</p>



<a name="227868689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868689">(Feb 26 2021 at 01:31)</a>:</h4>
<p>On older processors you can get gcc to generate six terms or more</p>



<a name="227868769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868769">(Feb 26 2021 at 01:32)</a>:</h4>
<p>Six additions/subtractions? for 32 bit integers?</p>



<a name="227868799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868799">(Feb 26 2021 at 01:33)</a>:</h4>
<p>six shift/add combos</p>



<a name="227868859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868859">(Feb 26 2021 at 01:33)</a>:</h4>
<p>I think that covers a good chunk of all 32 bit values</p>



<a name="227868928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868928">(Feb 26 2021 at 01:34)</a>:</h4>
<p>It's a weird complexity measure though, I'm not exactly sure how to find the optimal representation</p>



<a name="227868949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868949">(Feb 26 2021 at 01:34)</a>:</h4>
<p>if you use only shift and add it's obvious by just reading the binary string but with subtraction you do a kind of run length encoding</p>



<a name="227868958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227868958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227868958">(Feb 26 2021 at 01:35)</a>:</h4>
<p>I see. That seems interesting enough that I might scribble and see if I can prove an upper bound of strictly necessary operations. I still feel like this should be possible with <code>log_2(bits)</code> adds/subs, but that's just, like, my intuition speaking.</p>



<a name="227869010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869010">(Feb 26 2021 at 01:35)</a>:</h4>
<p>Yes, log_2(bits) is an upper bound, because you can just read the constant as a sum of powers of two</p>



<a name="227869074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869074">(Feb 26 2021 at 01:36)</a>:</h4>
<p>I mean like for 32 bit integers, do it in &lt;= 5 adds/subs. For any given constant operand.</p>



<a name="227869153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869153">(Feb 26 2021 at 01:37)</a>:</h4>
<p>Also it gets even more fun when you consider that you can shift intermediates in the computation, so something like <code>a = x * (2^10 + 2^8 + 2^2 + 1)</code> can be calculated as <code>y = x * (2^2 + 1); a = y * (2^8 + 1)</code></p>



<a name="227869202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869202">(Feb 26 2021 at 01:38)</a>:</h4>
<p>You mentioned you saw 6 steps so that smells like gcc utilizing heuristics and those are not achieving an optimal solution in all instances.</p>



<a name="227869235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869235">(Feb 26 2021 at 01:38)</a>:</h4>
<p>Well even with 6 steps you will be hard pressed to hit all 2^32 possible constant values</p>



<a name="227869240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869240">(Feb 26 2021 at 01:38)</a>:</h4>
<p>that's a lot of entropy</p>



<a name="227869285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869285">(Feb 26 2021 at 01:39)</a>:</h4>
<p>with the basic shift/add approach you can need anywhere from 1 to 32 steps</p>



<a name="227869390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869390">(Feb 26 2021 at 01:40)</a>:</h4>
<p>This seems like a perfect opportunity for another very nice approach to trying these things out – exhaustive search over a small problem size!</p>



<a name="227869452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869452">(Feb 26 2021 at 01:41)</a>:</h4>
<p>so for instance, if you're verifying your strength reduction is correct, you can see if you can prove its correct for u8 or u16 by trying _all_ values, and the same algorithm will most likely be equally correct for u32/u64/u128 etc.</p>



<a name="227869455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869455">(Feb 26 2021 at 01:41)</a>:</h4>
<p>With the reuse of intermediates it looks a lot like the <a href="https://en.wikipedia.org/wiki/Addition-chain_exponentiation">addition-chain exponentiation</a> problem</p>



<a name="227869914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227869914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227869914">(Feb 26 2021 at 01:47)</a>:</h4>
<p>Wow, this stuff is over my head <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="227870048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870048">(Feb 26 2021 at 01:49)</a>:</h4>
<p>This looks like it might be the trick LLVM uses: <a href="https://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs">https://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs</a></p>
<h1>Compute the integer absolute value (abs) without branching</h1>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="kt">int</span> <span class="n">v</span><span class="p">;</span>           <span class="c1">// we want to find the absolute value of v</span>
<span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">r</span><span class="p">;</span>  <span class="c1">// the result goes here</span>
<span class="kt">int</span> <span class="k">const</span> <span class="n">mask</span> <span class="o">=</span> <span class="n">v</span> <span class="o">&gt;&gt;</span> <span class="k">sizeof</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="o">*</span> <span class="n">CHAR_BIT</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>

<span class="n">r</span> <span class="o">=</span> <span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="n">mask</span><span class="p">)</span> <span class="o">^</span> <span class="n">mask</span><span class="p">;</span>
</code></pre></div>
<p>Patented variation:</p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="n">r</span> <span class="o">=</span> <span class="p">(</span><span class="n">v</span> <span class="o">^</span> <span class="n">mask</span><span class="p">)</span> <span class="o">-</span> <span class="n">mask</span><span class="p">;</span>
</code></pre></div>



<a name="227870147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870147">(Feb 26 2021 at 01:50)</a>:</h4>
<p>When they say "Patented variation", do they mean literally patented, as in patent office?</p>



<a name="227870153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870153">(Feb 26 2021 at 01:50)</a>:</h4>
<blockquote>
<p>problem of finding a shortest addition chain for a given set of exponents has been proven NP-complete.</p>
</blockquote>
<p>Welp, I guess?</p>



<a name="227870267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870267">(Feb 26 2021 at 01:52)</a>:</h4>
<p>Time to learn a habit of proving my problems aren't NP-complete before I attempt to solve the problems themselves.</p>



<a name="227870343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870343">(Feb 26 2021 at 01:53)</a>:</h4>
<p>Some time in the SAT solver industry has taught me to take "NP-complete" more as a challenge than a "do not enter" sign</p>



<a name="227870425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870425">(Feb 26 2021 at 01:54)</a>:</h4>
<p>My instructor had the saying "NP is the new P"</p>



<a name="227870426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870426">(Feb 26 2021 at 01:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227868311">said</a>:</p>
<blockquote>
<p>yeah like <code>7x = 8x - x</code> reduces number of terms/operations.</p>
</blockquote>
<p>Are you saying replace <code>7*x</code> with <code>8*x - x</code>, or the reverse? Because replacing <code>7*x</code> with <code>8*x - x</code> seems like it would be slower.</p>



<a name="227870484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870484">(Feb 26 2021 at 01:55)</a>:</h4>
<p><code>8*x - x</code> is faster because it can be calculated as <code>x &lt;&lt; 3 - x</code></p>



<a name="227870497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870497">(Feb 26 2021 at 01:55)</a>:</h4>
<p>Huh, but doesn't that use an extra register?</p>



<a name="227870502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870502">(Feb 26 2021 at 01:55)</a>:</h4>
<p>assuming that the shift and sub instructions are much faster than imul</p>



<a name="227870510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870510">(Feb 26 2021 at 01:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227870343">said</a>:</p>
<blockquote>
<p>Some time in the SAT solver industry has taught me to take "NP-complete" more as a challenge than a "do not enter" sign</p>
</blockquote>
<p>Does SAT mean <a href="https://en.wikipedia.org/wiki/Boolean_satisfiability_problem">https://en.wikipedia.org/wiki/Boolean_satisfiability_problem</a>?</p>



<a name="227870550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870550">(Feb 26 2021 at 01:56)</a>:</h4>
<p>yes</p>



<a name="227870582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870582">(Feb 26 2021 at 01:56)</a>:</h4>
<p>see in particular <a href="https://en.wikipedia.org/wiki/Boolean_satisfiability_problem#Algorithms_for_solving_SAT">https://en.wikipedia.org/wiki/Boolean_satisfiability_problem#Algorithms_for_solving_SAT</a></p>



<a name="227870592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870592">(Feb 26 2021 at 01:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227870502">said</a>:</p>
<blockquote>
<p>assuming that the shift and sub instructions are much faster than imul</p>
</blockquote>
<p>I was trying to read the 8086 manual the other day to understand what each cycle of an instruction does, but I'm guessing it's proprietary.</p>



<a name="227870635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870635">(Feb 26 2021 at 01:57)</a>:</h4>
<p>Agner Fog has some public measurements of the latency and throughput of all the instructions on all the hardware</p>



<a name="227870644"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870644" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870644">(Feb 26 2021 at 01:58)</a>:</h4>
<p><a href="https://www.agner.org/optimize/instruction_tables.pdf">https://www.agner.org/optimize/instruction_tables.pdf</a></p>



<a name="227870712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870712">(Feb 26 2021 at 01:58)</a>:</h4>
<p>most of x86 hardware (you want the document 4. Instruction tables: Lists of instruction latencies, throughputs and micro-operation breakdowns for Intel, AMD and VIA CPUs)</p>



<a name="227870776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870776">(Feb 26 2021 at 01:59)</a>:</h4>
<p>Over time I found that its easy to misinterpret these tables, but they should give you a broad idea of how instructions stack up against each other regardless.</p>



<a name="227870862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870862">(Feb 26 2021 at 02:00)</a>:</h4>
<p>Yeah, I'm just curious :)</p>



<a name="227870871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870871">(Feb 26 2021 at 02:00)</a>:</h4>
<p>406 pages!</p>



<a name="227870876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227870876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227870876">(Feb 26 2021 at 02:00)</a>:</h4>
<p>What kind of issues do you run into?</p>



<a name="227871033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871033">(Feb 26 2021 at 02:02)</a>:</h4>
<p>Well, its easy for the actual measurements not really match up with the numbers from these tables in real world operation due to pipelining and the fact that CPUs have multiple execution units that are able to do variety of different things. And then the fact that you can have stalls and often need to wait for instructions to retire etc.</p>



<a name="227871208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871208">(Feb 26 2021 at 02:04)</a>:</h4>
<p>I was wondering what µops were (micro-operations, I assume), so I looked up "microps" and found several photos of fish. Apparently <em>Pareiodon microps</em> is a species of catfish <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="227871219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871219">(Feb 26 2021 at 02:04)</a>:</h4>
<p>Probably should have searched for "micro-ops"</p>



<a name="227871265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871265">(Feb 26 2021 at 02:05)</a>:</h4>
<p>I much prefer looking at llvm-mca's output (though having information that's in Agner's tables is a prerequisite to do analysis mca does)</p>



<a name="227871266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871266">(Feb 26 2021 at 02:05)</a>:</h4>
<p>x86 is way too complicated due to backwards compatibility stuff, so intel processors basically do jit compilation during execution</p>



<a name="227871293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871293">(Feb 26 2021 at 02:05)</a>:</h4>
<p>Hold on let me find the movinator lol</p>



<a name="227871298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871298">(Feb 26 2021 at 02:05)</a>:</h4>
<p>lolol</p>



<a name="227871393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871393">(Feb 26 2021 at 02:06)</a>:</h4>
<p>Sorry, movfuscator<br>
<a href="https://github.com/xoreaxeaxeax/movfuscator">https://github.com/xoreaxeaxeax/movfuscator</a></p>



<a name="227871434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871434">(Feb 26 2021 at 02:07)</a>:</h4>
<p>Btw, why does it tend to be that the more cores a machine has, the lower the clock speed? Is it because of synchronization?</p>



<a name="227871475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871475">(Feb 26 2021 at 02:07)</a>:</h4>
<p>power, heat. Clocks don't need to be synchronized between cores, they communicate over a bus.</p>



<a name="227871543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871543">(Feb 26 2021 at 02:08)</a>:</h4>
<p>Hm, so otherwise the machine might overheat?</p>



<a name="227871547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871547">(Feb 26 2021 at 02:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227871393">said</a>:</p>
<blockquote>
<p>Sorry, movfuscator<br>
<a href="https://github.com/xoreaxeaxeax/movfuscator">https://github.com/xoreaxeaxeax/movfuscator</a></p>
</blockquote>
<p>Wow, that's cool!</p>



<a name="227871555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871555">(Feb 26 2021 at 02:08)</a>:</h4>
<p>You can really turn branches into <code>mov</code>s?</p>



<a name="227871581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871581">(Feb 26 2021 at 02:09)</a>:</h4>
<p>I think branches are done with conditional moves on everything in the not-taken branch</p>



<a name="227871697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871697">(Feb 26 2021 at 02:10)</a>:</h4>
<p>Doesn't look that way, at least not for this example: </p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="kt">int</span> <span class="nf">is_prime</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">==</span><span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="o">=</span><span class="mi">2</span><span class="p">;</span> <span class="n">i</span><span class="o">*</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">x</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">%</span><span class="n">i</span><span class="o">==</span><span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>mov asm: <a href="https://github.com/xoreaxeaxeax/movfuscator/blob/master/overview/prime_mov.asm">https://github.com/xoreaxeaxeax/movfuscator/blob/master/overview/prime_mov.asm</a></p>



<a name="227871815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871815">(Feb 26 2021 at 02:11)</a>:</h4>
<p>There's a trick to encode conditional movs by moving either a dummy value or the real value and using memory addressing to pick the right one</p>



<a name="227871909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871909">(Feb 26 2021 at 02:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227871543">said</a>:</p>
<blockquote>
<p>Hm, so otherwise the machine might overheat?</p>
</blockquote>
<p>power/tdp is just an arbitrary design constraint, ways we have to deal with the heat dissipation is only one of the considerations that go into making such a constraint. If nothing was a concern, engineers could design chips that consume/dissipate thousands of watts of heat and in doing so run very many cores very fast, if they wanted to.</p>



<a name="227871961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871961">(Feb 26 2021 at 02:13)</a>:</h4>
<p>Christopher Domas has a presentation on it somewhere. (I highly recommend all his talks BTW, he's a pretty amazing reverse engineer)</p>



<a name="227871988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871988">(Feb 26 2021 at 02:14)</a>:</h4>
<p><a href="https://www.youtube.com/watch?v=2VF_wPkiBJY">https://www.youtube.com/watch?v=2VF_wPkiBJY</a></p>
<div class="youtube-video message_inline_image"><a data-id="2VF_wPkiBJY" href="https://www.youtube.com/watch?v=2VF_wPkiBJY"><img src="https://i.ytimg.com/vi/2VF_wPkiBJY/default.jpg"></a></div>



<a name="227871990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227871990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227871990">(Feb 26 2021 at 02:14)</a>:</h4>
<p>Btw, do you know of any good resources to start learning about making digital circuits? I tried to make an AND gate once but it didn't work quite right :)</p>



<a name="227872185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872185">(Feb 26 2021 at 02:16)</a>:</h4>
<p>(My [extreme] stretch goal is to build a RISC-V CPU and run Rust on it <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span> <span aria-label="crab" class="emoji emoji-1f980" role="img" title="crab">:crab:</span>)</p>



<a name="227872205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872205">(Feb 26 2021 at 02:16)</a>:</h4>
<p>Btw, thanks for all the helpful resources and interesting conversation :)</p>



<a name="227872250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872250">(Feb 26 2021 at 02:17)</a>:</h4>
<p>The clock speed is determined by the propagation delay of the most complicated circuit. So if you want a one clock cycle multiplier then your clock speed will be slow. This is improved by pipelining, and modern machines are highly pipelined, but you need registers to buffer stuff between each stage and eventually the switching overhead of those buffers dominates and you don't make any more progress by pipelining further</p>



<a name="227872326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872326">(Feb 26 2021 at 02:18)</a>:</h4>
<p>Also it's really hard to write one fifth of an adder</p>



<a name="227872385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872385">(Feb 26 2021 at 02:19)</a>:</h4>
<p>Imagine writing an async function where every fourth word in the code has to be <code>.await</code></p>



<a name="227872458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872458">(Feb 26 2021 at 02:20)</a>:</h4>
<p>Asynchronous circuits ftw!</p>



<a name="227872735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872735">(Feb 26 2021 at 02:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227872250">said</a>:</p>
<blockquote>
<p>The clock speed is determined by the propagation delay of the most complicated circuit. So if you want a one clock cycle multiplier then your clock speed will be slow. This is improved by pipelining, and modern machines are highly pipelined, but you need registers to buffer stuff between each stage and eventually the switching overhead of those buffers dominates and you don't make any more progress by pipelining further</p>
</blockquote>
<p>Thanks, that helps clarify it!</p>



<a name="227872793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872793">(Feb 26 2021 at 02:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227872326">said</a>:</p>
<blockquote>
<p>Also it's really hard to write one fifth of an adder</p>
</blockquote>
<p>Hmm, what do you mean? Say <code>add</code> takes 5 cycles—doesn't mean there are five fifths of an adder?</p>



<a name="227872807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872807">(Feb 26 2021 at 02:25)</a>:</h4>
<p>Yes, and they are all separate "functions"</p>



<a name="227872861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872861">(Feb 26 2021 at 02:26)</a>:</h4>
<p>so you have to carefully write the logic so that it can be partitioned into 5 equal sized pieces</p>



<a name="227872891"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227872891" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227872891">(Feb 26 2021 at 02:26)</a>:</h4>
<p>which means that you are usually <code>.await</code>ing at really inconvenient points</p>



<a name="227873069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873069">(Feb 26 2021 at 02:29)</a>:</h4>
<p>Huh, I didn't realize that's how multi-cycle instructions work.</p>



<a name="227873075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873075">(Feb 26 2021 at 02:29)</a>:</h4>
<p>That seems incredibly hard <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>



<a name="227873297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873297">(Feb 26 2021 at 02:32)</a>:</h4>
<p>So there's basically <code>add1</code>, <code>add2</code>, <code>add3</code>, <code>add4</code>, and <code>add5</code>, and every clock cycle the next is called?</p>



<a name="227873301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873301">(Feb 26 2021 at 02:32)</a>:</h4>
<p>yes</p>



<a name="227873323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873323">(Feb 26 2021 at 02:32)</a>:</h4>
<p>the pipelining means that actually all five pieces are being used at the same time for different data</p>



<a name="227873374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873374">(Feb 26 2021 at 02:33)</a>:</h4>
<p>Huh, so because it's in multiple pieces, multiple numbers can be going through at the same time?</p>



<a name="227873384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873384">(Feb 26 2021 at 02:33)</a>:</h4>
<p>Wow</p>



<a name="227873444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873444">(Feb 26 2021 at 02:34)</a>:</h4>
<p>All I can say is, compared to this, configuring DNS is easy <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="227873576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227873576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227873576">(Feb 26 2021 at 02:37)</a>:</h4>
<p>Thanks again for all the fascinating information :)</p>



<a name="227874013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227874013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227874013">(Feb 26 2021 at 02:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic/near/227870497">said</a>:</p>
<blockquote>
<p>Huh, but doesn't that use an extra register?</p>
</blockquote>
<p>Big desktop chips do register renaming and have huge register files internally, so that's usually not an important concern.</p>



<a name="227874127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227874127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227874127">(Feb 26 2021 at 02:46)</a>:</h4>
<p>So many layers of abstraction <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="227874141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227874141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227874141">(Feb 26 2021 at 02:46)</a>:</h4>
<p>well, for the compiler optimization it does affect register pressure since they can only use the registers in the ISA, but it just gets folded in with everything else</p>



<a name="227911538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227911538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Olivier FAURE <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227911538">(Feb 26 2021 at 11:01)</a>:</h4>
<blockquote>
<p>Big desktop chips do register renaming and have huge register files internally, so that's usually not an important concern.</p>
</blockquote>
<p>One thing I've wondered for a while is why there is no ISA that gives explicit access to these register files. I get that there are instruction size limits, but you could always have special "store in / load from extended register set" instructions, that would get elided by register renaming during pipelining.</p>



<a name="227913989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227913989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227913989">(Feb 26 2021 at 11:25)</a>:</h4>
<p>The more exposed registers there are, the less freedom the cpu has to rename registers as it has to preserve all exposed registers. For example it can store results of speculatively executed instructions in non-exposed registers and discard them if the instruction never retired.</p>



<a name="227914159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914159">(Feb 26 2021 at 11:26)</a>:</h4>
<p>There's also ISA portability – you must have silicon for each register you have in the ISA, but you don't necessarily want to put as many registers in a laptop chip as you do in a server chip.</p>



<a name="227914189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914189">(Feb 26 2021 at 11:27)</a>:</h4>
<p>Furthermore, register renaming is a well explored algorithm that's easy and cheap to implement AFAIK.</p>



<a name="227914224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914224">(Feb 26 2021 at 11:27)</a>:</h4>
<p>If you make the API leak architectural details then it becomes a lot harder to program. I think there are some architectures that have attempted this but it's a very different ballgame</p>



<a name="227914234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914234">(Feb 26 2021 at 11:27)</a>:</h4>
<p>Delegating responsibility of managing this kind of things to the compiler doesn't help. Itanium tried it, but failed. A big reason for this is that for many things the most efficient decision can only be made at runtime and not statically.</p>



<a name="227914269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914269">(Feb 26 2021 at 11:27)</a>:</h4>
<p>The Mill architecture is an interesting exploration in this direction</p>



<a name="227914406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914406">(Feb 26 2021 at 11:28)</a>:</h4>
<p>they have a bunch of youtube videos about the design and it's really different from the usual, but they don't have any hardware yet</p>



<a name="227914626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914626">(Feb 26 2021 at 11:30)</a>:</h4>
<p>I used to be super excited about Mill, but I haven't heard anything about Mill for years now.</p>



<a name="227914659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914659">(Feb 26 2021 at 11:31)</a>:</h4>
<p>yeah I doubt they will actually deliver, but I like the ideas anyway</p>



<a name="227914665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914665">(Feb 26 2021 at 11:31)</a>:</h4>
<p>I asked at some point if they ran out of steam, was told its still being worked on, so perhaps they turned in a less public direction or something, idk.</p>



<a name="227914768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227914768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227914768">(Feb 26 2021 at 11:32)</a>:</h4>
<p>Someone should make a WASM machine.</p>



<a name="227983028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/llvm%20abs%20intrinsic/near/227983028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/llvm.20abs.20intrinsic.html#227983028">(Feb 26 2021 at 19:54)</a>:</h4>
<p>Heh, that made me think: WAT looks like Lisp and so it would be a little bit like a return to a Lisp machine <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>